package com.huarun.module.repair.controller;

import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huarun.common.BaseController;
import com.huarun.common.ResponseMessage;
import com.huarun.common.util.Constant;
import com.huarun.common.util.HttpUtil;
import com.huarun.common.util.ResultCodeEnum;
import com.huarun.common.util.TokenUtil;
import com.huarun.module.repair.bean.RepairRated;
import com.huarun.module.repair.service.IRepairRatedService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author hqy
 * @创建时间 2018-10-26 15:38
 * @Description 报修点评
 */
@Api(description = "报修点评")
@Controller
@RequestMapping("/wx/repairRated")
public class RepairRatedController extends BaseController {
	@Resource
	private IRepairRatedService repairRatedService;

	@ApiOperation(value = "故障类型", notes = "故障类型")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Object Project_WorkOrders(HttpServletRequest request) throws Exception {
		long time = System.currentTimeMillis();
		String url = "https://zhyy.crland.com.cn:8082/HuaRunCity.API/api/v1/Project_WorkOrders/GetFailureTypeData?appkey={"
				+ Constant.MANAGE_APPKEY + "}&time={" + time + "}&token={"
				+ TokenUtil.createToken(null, request, time, new HashMap<>()) + "}";
		String data = HttpUtil.doGet(url);
		return getOperResultMap("故障类型", data);
	}

	@ApiOperation(value = "报修点评", notes = "报修点评")
	@RequestMapping(value = "/evaluation", method = RequestMethod.GET)
	@ResponseBody
	public ResponseMessage<Object> Project_WorkOrdersEvaluation(HttpServletRequest request,
			@RequestParam(required = true) String workOrdersGuid, @RequestParam(required = true) Integer rated,
			@RequestParam(required = false) String review) throws Exception {
		ResponseMessage<Object> rm = new ResponseMessage<>();
		try {
			/*if (null != repairRatedService.getByOrderNo(workOrdersGuid)) {
				rm.setCodeAndMsg(ResultCodeEnum.ERROR.code, "此单已点评，无需重复操作");
			} else {*/
			RepairRated entity = new RepairRated();
			RepairRated repairRated = repairRatedService.getByOrderNo(workOrdersGuid);
			if (null != repairRated) {
				entity.setId(repairRated.getId());
			}
			entity.setOrderNo(workOrdersGuid);
			entity.setRated(rated);
			entity.setReview(review);
			int count = repairRatedService.save(entity);
			log.error("更新{}条数据！", count);
			rm.setCodeAndMsg(ResultCodeEnum.SUCCESS.code, ResultCodeEnum.SUCCESS.msg);
			//}
		} catch (Exception e) {
			rm.setCodeAndMsg(ResultCodeEnum.ERROR.code, ResultCodeEnum.ERROR.msg);
			log.error("报修点评异常！");
			e.printStackTrace();
		}
		return rm;
	}

	@ApiOperation(value = "点评查看", notes = "点评查看")
	@RequestMapping(value = "/evaluationDetail", method = RequestMethod.GET)
	@ResponseBody
	public ResponseMessage<RepairRated> Project_WorkOrdersEvaluationDetail(HttpServletRequest request,
			@RequestParam(required = true) String workOrdersGuid) throws Exception {
		ResponseMessage<RepairRated> rm = new ResponseMessage<RepairRated>();
		try {
			RepairRated entity = new RepairRated();
			// String workOrdersGuid=request.getParameter("workOrdersGuid");
			entity = repairRatedService.getByOrderNo(workOrdersGuid);
			rm.setCodeAndMsg(ResultCodeEnum.SUCCESS.code, ResultCodeEnum.SUCCESS.msg);
			rm.setData(entity);
		} catch (Exception e) {
			rm.setCodeAndMsg(ResultCodeEnum.ERROR.code, ResultCodeEnum.ERROR.msg);
			log.error("报修点评查看异常！");
			e.printStackTrace();
		}
		return rm;
	}
}
